﻿using System;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Xml.Serialization;
using KaupischITC.CardReader.Results.Egk.AllgemeineVersicherungsdaten;
using KaupischITC.CardReader.Results.Egk.GeschuetzteVersichertendaten;
using KaupischITC.CardReader.Results.Egk.PersoenlicheVersichertendaten;


namespace KaupischITC.CardReader.Results
{
	public class EgkResult
	{		
		public PersoenlicheVersichertendaten PersoenlicheVersichertendaten { get; private set; }
		public AllgemeineVersicherungsdaten AllgemeineVersicherungsdaten{ get; private set; }
		public GeschuetzteVersichertendaten GeschuetzteVersichertendaten { get;private set; }


		public EgkResult(byte[] pdData,byte[] vdData)
		{
			this.DecodePD(pdData);
			this.DecodeVD(vdData);
		}

		
		private void DecodePD(byte[] bytes)
		{
			int length = (bytes[0]<<8) + bytes[1];

			byte[] compressedData = new byte[length];
			Array.Copy(bytes,2,compressedData,0,compressedData.Length);

			this.PersoenlicheVersichertendaten = this.Decompress<PersoenlicheVersichertendaten>(compressedData);
		}
		

		private void DecodeVD(byte[] bytes)
		{
			int offsetStartVD = (bytes[0]<<8) + bytes[1];
			int offsetEndVD = (bytes[2]<<8) + bytes[3];
			int offsetStartGVD = (bytes[4]<<8) + bytes[5];
			int offsetEndGVD = (bytes[6]<<8) + bytes[7];

			byte[] compressedDataVD = new byte[offsetEndVD-offsetStartVD];
			Array.Copy(bytes,offsetStartVD,compressedDataVD,0,compressedDataVD.Length);

		//	byte[] compressedDataGVD = new byte[offsetEndGVD-offsetStartGVD];
		//	Array.Copy(bytes,offsetStartGVD,compressedDataGVD,0,compressedDataGVD.Length);

			this.AllgemeineVersicherungsdaten = this.Decompress<AllgemeineVersicherungsdaten>(compressedDataVD);
		//	this.GeschuetzteVersichertendaten = this.Decompress<GeschuetzteVersichertendaten>(compressedDataGVD);
		}


		private T Decompress<T>(byte[] compressedData)
		{
			using (MemoryStream memoryStream = new MemoryStream(compressedData))
			using (GZipStream gzipStream = new GZipStream(memoryStream,CompressionMode.Decompress))
			using (StreamReader streamReader = new StreamReader(gzipStream,Encoding.GetEncoding("ISO-8859-15")))
			{
				XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
				return (T)xmlSerializer.Deserialize(streamReader);
			}
		}
	}
}
